package com.kylin.module.study.actions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.study.business.ArticleBlogic;
import com.kylin.module.study.forms.ArticleForm;
import com.kylin.module.study.orm.Article;
import com.kylin.module.study.orm.Vocabulary;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

/**
 * 学习部分文章功能的实现
 * 
 * @author DH
 * @version 01/22/08
 * @see com.kylin.struts.BaseDispatchAction
 * @see com.kylin.struts.BaseActionInterface
 */
public class ArticleAction extends BaseDispatchAction implements
		BaseActionInterface {

	protected static Logger log = Logger.getLogger(ArticleAction.class);
	/**
	 * 查询最新的10条文章
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward newList(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询最新的 文章 : ");
		String forward = "newArticleList";
		int size = 10;// 查询最新的 10条
		CriteriaInfo cinfo = null;
		try {
			Locale practLocal = (Locale) request.getSession().getAttribute(
					Constants.CURR_PRACTLANGUAGE);
			List conditions = new ArrayList();
			// 条件： Vocabulary.language = sessionLocale(文章所属语言 = 当前语言)
			conditions.add(new PropertyCondition("language", getSessionLocale(request).toString(), PropertyCondition.OPERATOR_EQ));
			if (practLocal != null) // // 条件： (文章的学习语言 = 当前学习语言)
				conditions.add(new PropertyCondition("targetlang", practLocal
						.toString(), PropertyCondition.OPERATOR_EQ));

			cinfo = new CriteriaInfo(Article.class, conditions);
			cinfo.setOrderby(Order.desc("createdon"));
			DisplayPage page = PageManager.getDisplayPage(size, 1, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		} catch (Exception e) {
			forward = doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 添加文章
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始添加新的文章");
		String forward = "addSucc";
		ArticleForm articleForm = (ArticleForm) actionForm;
		try {
			ArticleBlogic logic = new ArticleBlogic();
			logic.addArticle(articleForm);
			request.setAttribute(Constants.MSGKEY, new MsgKey("", "添加文章成功"));
			if("list".equals(request.getParameter("nextdo"))){
				forward = "list";
				int pageSize = 10;
				int pageNum = 1;
				List conditions = new LinkedList();
				// 构造查询条件:母语
				String language = request.getParameter("language");
				if (language != null && !language.equals("")) {
					PropertyCondition cond = new PropertyCondition("language",
							language, PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}
				CriteriaInfo cinfo = new CriteriaInfo(Article.class, conditions);
				cinfo.setOrderby(Order.desc("createdon"));
				addUserCacheCriteriaInfo(request, cinfo);
				DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,
						cinfo);
				request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
			}
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("删除文章：" + id);
		try {
			ArticleBlogic logic = new ArticleBlogic();
			logic.deleteById(new Long(id));
			request.setAttribute(Constants.MSGKEY, new MsgKey("", "删除文章成功"));
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return this.search(mapping, actionForm, request, response);
	}

	/**
	 * 查询文章列表 如果有查询条件则根据条件查询
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("查询文章");
		String forward = "list";
		String searchId = request.getParameter("searchId");
		int pageSize = 10;
		int pageNum = 1;
		CriteriaInfo cinfo = null;
		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				List conditions = new LinkedList();
				// 构造查询条件:母语
				String language = request.getParameter("language");
				if (language != null && !language.equals("")) {
					PropertyCondition cond = new PropertyCondition("language",
							language, PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}
				// 构造查询条件:学习语言
				String targetlang = request.getParameter("targetlang");
				if (targetlang != null && !targetlang.equals("")) {
					PropertyCondition cond = new PropertyCondition(
							"targetlang", targetlang,
							PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}
				// 构造查询条件:文章分类
				String catarticleId = request.getParameter("catarticle");
				if (catarticleId != null && !catarticleId.equals("")) {
					Dict catarticle = new Dict(new Long(catarticleId));
					PropertyCondition cond = new PropertyCondition(
							"catarticle", catarticle,
							PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}
				// 构造查询条件:标题
				String title = request.getParameter("keyword_title");
				if (title != null && !title.equals("")) {
					PropertyCondition cond = new PropertyCondition("title",
							"%" + title + "%",
							PropertyCondition.OPERATOR_LIKE);
					conditions.add(cond);
				}
				// 构造查询条件:关键字,设定从内容中取
				String keyword = request.getParameter("keyword_content");
				if (keyword != null && !keyword.equals("")) {
					PropertyCondition cond = new PropertyCondition("content",
							"%" + keyword + "%",
							PropertyCondition.OPERATOR_LIKE);
					conditions.add(cond);
				}
				cinfo = new CriteriaInfo(Article.class, conditions);
				cinfo.setOrderby(Order.desc("id"));
				addUserCacheCriteriaInfo(request, cinfo);
			} else {
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum,
					cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 根据文章ID获得需要更新的文章
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("更新文章" + id);
		String forward = "edit";
		Article obj = null;
		try {
			if (id != null) {
				ArticleBlogic logic = new ArticleBlogic();
				obj = (Article) logic.getArticleById(new Long(id));
			}
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 根据文章ID更新文章
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward executeUpdate(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("更新文章" + id);
		String forward = "edit";
		ArticleForm form = (ArticleForm) actionForm;
		try {
			if (id != null) {
				ArticleBlogic logic = new ArticleBlogic();
				Article obj = logic.updateArticle(form);
				request.setAttribute(Constants.MSGKEY, new MsgKey("","修改学习文章成功"));
				request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);			
			}
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	/**
	 * 根据文章ID查看文章详细内容
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward detail(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("查看文章详细信息：" + id);
		String forward = "detail";
		Article obj = null;
		try {
			ArticleBlogic logic = new ArticleBlogic();
			obj = logic.getArticleById(new Long(id));
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

}